package JAVA_Singleton;


/**
 * 单例在多线程下的问题: 懒汉式初始化的线程安全
 */
public class Singleton {
    public static void main(String[] args) {
                MyThread t1=new MyThread();
                MyThread t2=new MyThread();
                MyThread t3=new MyThread();
                t1.start();
                t2.start();
                t3.start();
    }
}

class SingletonTest{
    private static SingletonTest instance = null;
    public static void setInstance(SingletonTest instance) {
        SingletonTest.instance = instance;
    }
    private SingletonTest(){}
    public static SingletonTest getInstance(){
        if (instance == null) {
            synchronized (SingletonTest.class) {
                if (instance == null) {
                    instance = new SingletonTest();
                }
            }
        }
        return instance ;
    }
}
class MyThread extends Thread{
    @Override
    public void run() {
            System.out.println(SingletonTest.getInstance().hashCode());
    }


}